package com.cskaoyan.gateway.controller.shopping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.gateway.entity.AddCartRequestParam;
import com.cskaoyan.gateway.form.shopping.CartForm;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.CartShopService;
import com.mall.shopping.ICartService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.*;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.intercepter.TokenIntercepter;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author: 逍遥子
 * @Date: 2022/01/19/22:45
 * @Description: 购物车模块
 */
@Slf4j
@RestController
@RequestMapping("shopping")
public class CartController {

    @Reference(timeout = 5000, check = false)
    CartShopService cartShopService;

    // 购物车列表
    @GetMapping("carts")
    public ResponseData cartList(HttpServletRequest request){
        // 1.获取用户id
        String userInfo = (String) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);
        Long userId = Long.parseLong(object.get("uid").toString());
        System.out.println(userId);
        // 2.封装成Request对象, 传给Service层
        CartListByIdRequest cartListByIdRequest = new CartListByIdRequest();
        cartListByIdRequest.setUserId(userId);
        // 4.处理请求
        CartListByIdResponse response = cartShopService.getCartListById(cartListByIdRequest);
        // 5.响应内容
        if(ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())){
            // 返回用户购物车中的商品信息
            return new ResponseUtil().setData(response.getCartProductDtos());
        }
        // 返回错误提示
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    // 添加商品到购物车
    @PostMapping("carts")
    public ResponseData addProductToCart(@RequestBody AddCartRequestParam addCartRequestParam){

        Long productId = addCartRequestParam.getProductId();
        Long userId = addCartRequestParam.getUserId();
        Integer productNum = addCartRequestParam.getProductNum();

        // 1.封装成Request对象, 传给Service层
        AddCartRequest addCartRequest = new AddCartRequest();
        addCartRequest.setUserId(userId);
        addCartRequest.setItemId(productId);
        addCartRequest.setNum(productNum);
        // 2.处理请求
        AddCartResponse response = cartShopService.addToCart(addCartRequest);
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            // 成功处理请求
            return new ResponseUtil().getResponseData();
        }
        // 返回错误提示
        return new ResponseUtil().setErrorMsg(response.getMsg());

    }

    // 更新购物车中的商品数量
    @PutMapping("carts")
    public ResponseData updateProductNumOfCart(@RequestBody AddCartRequestParam addCartRequestParam){

        Long productId = addCartRequestParam.getProductId();
        Long userId = addCartRequestParam.getUserId();
        Integer productNum = addCartRequestParam.getProductNum();
        String checked = addCartRequestParam.getChecked();
        System.out.println("网关>>>" + checked);

        // 1.封装成Request对象, 传给Service层
        UpdateCartNumRequest updateCartNumRequest = new UpdateCartNumRequest();
        updateCartNumRequest.setUserId(userId);
        updateCartNumRequest.setItemId(productId);
        updateCartNumRequest.setNum(productNum);
        updateCartNumRequest.setChecked(checked);
        // 2.处理请求
        UpdateCartNumResponse response = cartShopService.updateCartNum(updateCartNumRequest);
        if(ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())){
            // 成功处理请求
            return new ResponseUtil().getResponseData();
        }
        // 返回错误提示
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    // 删除购物车中的商品
    @DeleteMapping("carts/{uid}/{pid}")
    public ResponseData deleteCartShop(@PathVariable Long uid, @PathVariable Long pid) {
        // 1.接收请求参数, 封装成Request对象, 传给Service层
        DeleteCartShopRequest deleteCartShopRequest = new DeleteCartShopRequest();
        deleteCartShopRequest.setUserId(uid);
        deleteCartShopRequest.setProductId(pid);
        // 2.处理请求
        DeleteCartShopResponse response = cartShopService.deleteCartShop(deleteCartShopRequest);
        // 3.根据Service层的响应码, 给前端返回提示信息
        if(ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())){
            // 删除成功
            return new ResponseUtil<String>().setData("删除成功");
        }
        // 返回错误提示
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    // 删除购物车中选中的商品
    @DeleteMapping("items/{uid}")
    public ResponseData deleteCartShopOfSelected(@PathVariable Long uid){
        // 1.接收请求参数, 封装成Request对象, 传给Service层
        DeleteCartShopOfSelectedRequest deleteCartShopOfSelectedRequest = new DeleteCartShopOfSelectedRequest();
        deleteCartShopOfSelectedRequest.setUserId(uid);
        // 2.处理请求
        DeleteCartShopOfSelectedResponse response = cartShopService.deleteCartShopOfSelected(deleteCartShopOfSelectedRequest);
        // 3.根据Service层返回的响应码, 给前端返回提示信息
        if(ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())){
            // 删除成功
            return new ResponseUtil<String>().setData(null);
        }
        // 返回错误提示
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    // 全选/全不选购物车商品
    @PutMapping("items")
    public ResponseData selectCartShop(@RequestBody Map map){
        long userId = Long.parseLong(map.get("userId").toString());
        Boolean checked = Boolean.getBoolean(map.get("checked").toString());
        // 1.接收请求参数, 封装成Request对象, 传给Service层
        SelectCartShopRequest selectCartShopRequest = new SelectCartShopRequest();
        selectCartShopRequest.setUserId(userId);
        selectCartShopRequest.setChecked(checked);
        // 2.处理请求
        SelectCartShopResponse response = cartShopService.selectCartShop(selectCartShopRequest);
        // 3.根据Service层返回的响应码, 给前端返回提示信息
        if(ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())){
            // 请求成功提示
            return new ResponseUtil().getResponseData();
        }
        // 返回错误提示
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }


   /* @Reference(timeout = 3000,retries = 2, check = false)
    ICartService cartService;*/

    /**
     * @description
     * @author WangZhe
     * @date 2022/1/20 15:44
     * @param
     * @param request
     * @return com.mall.commons.result.ResponseData
     */
   /* @GetMapping("carts")
    public ResponseData getCartListById(HttpServletRequest request) {
        String userInfo = (String) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());

        CartListByIdRequest cartListByIdRequest = new CartListByIdRequest();
        cartListByIdRequest.setUserId(uid);

        CartListByIdResponse response = cartService.getCartListById(cartListByIdRequest);

        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getCartProductDtos());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @PostMapping("carts")
    public ResponseData addCart(@RequestBody CartForm form, HttpServletRequest request){
//        String attribute = (String) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
//        JSONObject parse = (JSONObject) JSON.parse(attribute);
//        Long uid = Long.parseLong(parse.get("uid").toString());

        AddCartRequest addCartRequest = new AddCartRequest();

        addCartRequest.setUserId(form.getUserId());
        addCartRequest.setItemId(form.getProductId());
        addCartRequest.setNum(form.getProductNum());

        AddCartResponse addCartResponse = cartService.addToCart(addCartRequest);

        if (!addCartResponse.getCode().equals(ShoppingRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setErrorMsg(addCartResponse.getMsg());
        }

        return new ResponseUtil<>().setData("成功");
    }

    @PutMapping("carts")
    public ResponseData updateCart(@RequestBody CartForm cartForm, HttpServletRequest request){
//        String attribute = (String) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
//        JSONObject parse = (JSONObject) JSON.parse(attribute);
//        Long uid = Long.parseLong(parse.get("uid").toString());

        UpdateCartNumRequest cartNumRequest = new UpdateCartNumRequest();
        cartNumRequest.setChecked(cartForm.getChecked());
        cartNumRequest.setItemId(cartForm.getProductId());
        cartNumRequest.setUserId(cartForm.getUserId());
        cartNumRequest.setNum(cartForm.getProductNum());

        UpdateCartNumResponse updateCartNum = cartService.updateCartNum(cartNumRequest);

        if (!updateCartNum.getCode().equals(ShoppingRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setErrorMsg(updateCartNum.getMsg());
        }
        return new ResponseUtil<>().setData("成功");
    }*/
}
